/*
 * Sniff.java
 *
 * Created on August 21, 2007, 5:55 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package org.xeerkat.xmpp.tools;

import java.io.StringWriter;
import java.net.URI;
import java.util.logging.Logger;
import org.infoset.xml.DestinationSource;
import org.infoset.xml.Element;
import org.infoset.xml.Infoset;
import org.infoset.xml.InfosetFactory;
import org.infoset.xml.Item;
import org.infoset.xml.ItemConstructor;
import org.infoset.xml.ItemDestination;
import org.infoset.xml.XMLException;
import org.infoset.xml.util.WriterItemDestination;
import org.xeerkat.xmpp.ClientConnection;
import org.xeerkat.xmpp.ID;
import org.xeerkat.xmpp.Protocol;

/**
 *
 * @author alex
 */
public class Sniff
{
   
   static Logger LOG = Logger.getLogger(Sniff.class.getName());
   /** Creates a new instance of Sniff */
   public Sniff()
   {
   }
   
   public static void main(String [] args)
   {
      if (args.length<2) {
         System.err.println("Usage: "+Roster.class.getName()+" {id} {password}");
         System.exit(1);
      }
      try {
         ID id = new ID(args[0]);
         id.setPassword(args[1]);
         final ClientConnection connection = ClientConnection.lookupByDomain(id.getDomain());
         connection.connect(id);

         connection.startReceiver(new DestinationSource() {
            public ItemDestination getDestination()
               throws XMLException 
            {
               final StringWriter w = new StringWriter();
               return new WriterItemDestination(w,"UTF-8") {
                  int level = 0;
                  public void send(Item item)
                     throws XMLException
                  {
                     switch (item.getType()) {
                        case ElementItem:
                           if (level==0) {
                              Element e = (Element)item;
                              if (e.getName().equals(Protocol.PRESENCE) && "probe".equals(e.getAttributeValue("type"))) {
                                 connection.sendPresence(true);
                              }
                           }
                           level++;
                           break;
                     }
                     super.send(item);
                     switch (item.getType()) {
                        case ElementEndItem:
                           level--;
                           if (level==0) {
                              LOG.info(w.toString());
                           }
                     }
                  }
               };
            }

            public ItemDestination getDestination(URI baseURI)
               throws XMLException {
               return getDestination();
            }

            public ItemConstructor createItemConstructor() {
               return InfosetFactory.getDefaultInfoset().createItemConstructor();
            }

            public Infoset getInfoset() {
               return InfosetFactory.getDefaultInfoset();
            }
         });
         connection.sendPresence(true);
         
         synchronized (connection) {
            connection.wait();
         }
         connection.sendPresence(false);
      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }
   
}
